<template>
  <div>
    <div class="app-container clearfix pageHeader">
      <div class="header fl">{{$route.meta.title}}</div>
    </div>

    <div class="app-container margin-content padding-content">
      <el-form
        label-position="right"
        :model="dataForm"
        ref="dataForm"
        label-width="80px">

        <el-row :gutter="48">
          <el-col :sm="24" :md="7" v-if="isAdmin">
            <el-form-item label="所属码头" prop="portId">
              <treeselect
                placeholder="请选择"
                :options="options"
                :disable-branch-nodes="true"
                value-consists-of="LEAF_PRIORITY"
                :close-on-select="true"
                :open-on-click="true"
                v-model="dataForm.portId"
                @select="selectPortId"/>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="7">
            <el-form-item label="作业日期" prop="time">
              <el-date-picker
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                unlink-panels
                value-format="yyyy-MM-dd"
                v-model="dataForm.time"
                :picker-options="pickerOptions"
                style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="7">
            <el-form-item label="处理情况" prop="dealStatus">
              <el-select v-model="dataForm.dealStatus" placeholder="请选择" clearable style="width: 100%;">
                <el-option key="0" label="未处理" value="0"></el-option>
                <el-option key="1" label="已处理" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="3">
            <el-button type="primary" @click="handleSearch">查询</el-button>
          </el-col>
        </el-row>

        <!--<div style="text-align: right">-->
        <!--<el-button type="primary" @click="handleSearch">查询</el-button>-->
        <!--</div>-->
      </el-form>
    </div>


    <div class="app-container margin-content padding-content">
      <div class="btn-wrapper" v-if="overTakeoutDays">
        <el-tag>未提离货物超过 {{overTakeoutDays}} 天</el-tag>
      </div>
      <el-table
        :key="tableKey"
        :data="tableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        :highlight-current-row="true"
        style="width: 100%;"
        :min-height="scrollHeight">

        <el-table-column label="集装箱号" property="recordNo" width="110" fixed="left">
          <template slot-scope="scope">
            <a @click="view(scope.row.recordNo)">{{scope.row.recordNo}}</a>
          </template>
        </el-table-column>
        <el-table-column label="进出口" property="ieFlag">
          <template slot-scope="scope">
            {{scope.row.ieFlag | ieFlagFilter}}
          </template>
        </el-table-column>
        <el-table-column label="作业类型" property="actType" width="130">
          <template slot-scope="scope">
            {{scope.row.actType | activityTypeFilter}}
          </template>
        </el-table-column>
        <el-table-column label="作业时间" property="actTime" width="150">
          <template slot-scope="scope">
            {{scope.row.actTime | parseTime}}
          </template>
        </el-table-column>
        <el-table-column label="重吉" property="leFlag">
          <template slot-scope="scope">
            {{scope.row.leFlag | leFlagFilter}}
          </template>
        </el-table-column>
        <el-table-column label="集装箱尺寸" property="sizeId"></el-table-column>
        <el-table-column label="堆场位置" property="yardCode"></el-table-column>
        <el-table-column label="超时天数" property="overdays"></el-table-column>
        <el-table-column label="处理情况" width="100" fixed="right">
          <template slot-scope="scope">
            <el-popover
              style="cursor:pointer"
              placement="left"
              title="处理情况"
              width="500"
              trigger="click"
              :disabled="!parseInt(scope.row.dealStatus)">

              <el-table :data="[scope.row]">
                <el-table-column property="recordNum" label="序列号"></el-table-column>
                <el-table-column property="operatorName" label="处理人"></el-table-column>
                <el-table-column property="dealTimeDesc" label="处理时间" width="130"></el-table-column>
                <el-table-column property="dealType" label="处理意见" v-if="!scope.row.dealDetail" show-overflow-tooltip>
                  <template slot-scope="scope">
                    {{ scope.row.dealType | dealTypeFilter}}
                  </template>
                </el-table-column>
                <el-table-column property="dealDetail" label="处理意见" v-if="scope.row.dealDetail"
                                 show-overflow-tooltip></el-table-column>
              </el-table>

              <el-tag type="success" slot="reference" v-if="parseInt(scope.row.dealStatus)">已处理</el-tag>
              <el-tag type="warning" slot="reference" v-else>未处理</el-tag>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" v-if="hasPermission([0,270,282,283])">
          <template slot-scope="scope">
            <a @click="handleUpdate({id:scope.row.id, dealRecordNo:scope.row.recordNo,recordSource:6})">处理</a>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container align-right" v-show="tableData.length > 0">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="dataForm.pageNum"
          :page-sizes="pageSizes"
          :page-size="dataForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 弹窗, 情况处理 -->
    <warning-handle v-if="handleVisible" ref="handle" @refresh="_otTakeoutFetchList()"></warning-handle>
    <!-- 弹窗, 最新动态 -->
    <cont-detail-dialog v-if="contDetailVisible" ref="contDetail"></cont-detail-dialog>
  </div>
</template>

<script>
  // api
  import { getPortTree } from '@/api/port'
  import { otTakeoutFetchList } from '@/api/storageWarning'
  import { getSettings } from '@/api/warningSettings'
  import { hasPermission } from '@/utils/permission' //判断是否有权限，返回bollean值

  // utils
  import { pickerOptions, getLately } from '@/utils'
  import { page } from '@/utils/page'
  import { modifyObj } from '@/utils/tools'
  // vuex
  import { mapMutations, mapGetters } from 'vuex'
  // components
  import Treeselect from '@riophae/vue-treeselect'
  import ContDetailDialog from '@/components/ContDetailDialog'
  import WarningHandle from '@/components/WarningHandle'

  const time = getLately(0)

  export default {
    name: 'noLiftOffWarning',
    data() {
      return {
        hasPermission: hasPermission,
        tableKey: 0,
        tableData: [],
        loading: false,
        scrollHeight: null,
        dataForm: {
          time: null,
          portId: null,
          dealStatus: null,
          pageNum: 1,
          pageSize: page.pageSize
        },
        pageSizes: page.pageSizes,
        total: 0,
        pickerOptions: {
          shortcuts: pickerOptions
        },
        options: [],
        handleVisible: false,
        contDetailVisible: false,
        overTakeoutDays: ''
      }
    },
    computed: {
      ...mapGetters([
        'isAdmin'
      ])
    },
    created() {
      this._getPortTree()
      this._otTakeoutFetchList()
      this.tableResize()
    },
    methods: {
      tableResize(){
        const _height = 380
        this.scrollHeight = document.body.scrollHeight - _height
        window.onresize = () => {
          this.scrollHeight = document.body.scrollHeight - _height
        }
      },
      _getPortTree() {
        getPortTree().then((res) => {
          this.options = modifyObj(res.data.data)
        })
      },
      _otTakeoutFetchList() {
        this.loading = true
        this.tableData = []
        const [beginTime, endTime] = this.dataForm.time || ''
        const params = {beginTime, endTime}
        const data = Object.assign({}, params, this.dataForm)

        otTakeoutFetchList(data).then((res) => {
          this.tableData = res.data.data.dataList
          this.total = res.data.data.total
          this.loading = false
        })
      },
      _getSettings() {
        getSettings({portId: this.dataForm.portId}).then((res) => {
          console.log(res.data.data)
          this.overTakeoutDays = res.data.data.overTakeoutDays
        })
      },
      selectPortId(node) {
        this.dataForm.portId = node.id
        this._getSettings()
      },
      handleSearch() {
        this.dataForm.pageNum = 1
        this._otTakeoutFetchList()
      },
      handleSizeChange(val) {
        this.dataForm.pageSize = val
        this._otTakeoutFetchList()
      },
      handleCurrentChange(val) {
        this.dataForm.pageNum = val
        this._otTakeoutFetchList()
      },
      view(id) {
        this.contDetailVisible = true
        this.$nextTick(() => {
          this.setCurrentId(id)
          this.$refs['contDetail'].init()
        })
      },
      handleUpdate({id, dealRecordNo, recordSource}) {
        this.handleVisible = true
        this.$nextTick(() => {
          this.setCurrentId({id, dealRecordNo, recordSource})
          this.$refs['handle'].init()
        })
      },
      ...mapMutations({
        setCurrentId: 'SET_CURRENTID'
      })
    },
    components: {
      Treeselect,
      ContDetailDialog,
      WarningHandle
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped="scope">

</style>
